﻿using MainFile;
using MainFrameProperty;
using MainInterface;
using MainProGrid;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;

namespace MainZYPT
{
    public class MoudleCompoent : IProperType,IBuilder
    {
        public string ID { get; set; }
        public string TypeName { get; set; }
        public string ChinaName { get; set; }
        public string Description { get; set; }
        public string GroupName { get; set; }
        public string CreateTime { get; set; }
        public List<UIProperty> Properties = new List<UIProperty>();
        public List<UIEvent> Events = new List<UIEvent>();
        public List<UIMethod> Methodes = new List<UIMethod>();
        public string Reference { get; set; }
        public string Links { get; set; }
        public string ImageUrl { get; set; }
        public Image Image { get; set; }
        public string File { get; set; }
        private XmlDocument Doc;
        public void LoadXmlZYK(XmlDocument doc)
        {
            this.Doc = doc;
            XmlNode root = doc.DocumentElement;
            XmlAttribute attr = root.Attributes["ID"];
            this.ID = attr.Value;
            attr = root.Attributes["TypeName"];
            this.TypeName = attr.Value;
            attr = root.Attributes["ChinaName"];
            this.ChinaName = attr.Value;
            attr = root.Attributes["Description"];
            this.Description = attr.Value;
            attr = root.Attributes["GroupName"];
            this.GroupName = attr.Value;
            attr = root.Attributes["CreateTime"];
            this.CreateTime = attr.Value;
            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name == "Properties") 
                {
                    LoadProperties(node);
                }
                else if (node.Name == "Events") 
                {
                    LoadEvents(node);
                }
                else if (node.Name == "Methodes") 
                {
                    LoadMethodes(node);
                }
                else if (node.Name == "Reference") 
                {
                    this.Reference = node.InnerText.Trim();
                }
                else if (node.Name == "Links")
                {
                    this.Links = node.InnerText.Trim();
                }
                else if (node.Name == "File")
                {
                    this.File = node.Attributes["Src"].Value.Trim();
                }
                else if (node.Name == "Image"&&this.Image==null)
                {
                    this.ImageUrl = node.Attributes["Src"].Value;
                    this.Image = MyFile.GetImageForUrl(this.ImageUrl);
                }
                else 
                {
                  
                }
            }
        }

        private void LoadMethodes(XmlNode node)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                UIMethod method = new UIMethod();
                method.LoadXmlZYK(item);
                this.Methodes.Add(method);
            }
        }

        private void LoadEvents(XmlNode node)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                UIEvent evt = new UIEvent();
                evt.LoadXmlZYK(item);
                this.Events.Add(evt);
            }
        }

        private void LoadProperties(XmlNode node)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                UIProperty pro = new UIProperty();
                pro.LoadXmlZYK(item);
                this.Properties.Add(pro);
            }
        }

        public MoudleCompoent Copy()
        {
            MoudleCompoent mc = new MoudleCompoent();
            mc.Image = this.Image;
            mc.ImageUrl = this.ImageUrl;
            mc.LoadXmlZYK(this.Doc);
            return mc;
        }

        public void AddPro(CustomSelectObject PMC)
        {
            CustomProperty pro = new CustomProperty(this, typeof(string), "引用", "Reference", "说明", "用于在生成页面时调用组件的库", true);
            PMC.Properties.Add(pro);
            pro = new CustomProperty(this, typeof(string), "组件说明", "Description", "说明", "用于说明组件的用途",true);
            PMC.Properties.Add(pro);
            pro = new CustomProperty(this, typeof(string), "组件中文名称", "ChinaName", "说明", "组件的中文名称", true);
            PMC.Properties.Add(pro);
            foreach (UIProperty item in Properties)
            {
                if (item.Value == null)
                {
                    continue;
                }
                pro = new CustomProperty(this,item.Value.GetType(),item.ChinaName, item.Name, item.GroupName, item.Description, false);
                PMC.Properties.Add(pro);
            }
            foreach (UIEvent item in Events)
            {

                CustomEvent etem = new CustomEvent(this, typeof(UIEvent), item.ChinaName, item.Name, item.Description, item.Args);
                PMC.Events.Add(etem);
            }
        }

        public object GetValue(string name)
        {
            object value = null;
            if (name == "Reference") 
            {
                return this.Reference;
            }
            if (name == "Description") 
            {
                return this.Description;
            }
            if (name == "ChinaName") 
            {
                return this.ChinaName;
            }
            foreach (var item in Properties)
            {
                if (item.Name == name) 
                {
                    value=item.Value;
                    break;
                }
            }
            if (value == null) 
            {
                foreach (var item in Events)
                {
                    if (item.Name == name) 
                    {
                        value=item.Value;
                        break;
                    } 
                }
            }
            return value;
        }

        public Boolean SetValue(string name, object value)
        {
            if (name == "Reference")
            {
                return true;
            }
            if (name == "Description")
            {
                return true;
            }
            if (name == "ChinaName")
            {
                return true;
            }
            foreach (var item in Properties)
            {
                if (item.Name == name)
                {
                    item.Value=value;
                    return true;
                }
            }
            foreach (var item in Events)
            {
                if (item.Name == name) 
                {
                    item.Value = value.ToString();
                    return true;
                }
            }
            return false;
        }

        public string GetDom(string dom)
        {
            return string.Empty;
        }

        public string GetCSS(string style)
        {
            return string.Empty;
        }

        public string GetScript(string script)
        {
            string tem = string.Empty;
            foreach (var item in Properties)
            {
                tem += item.GetScript(string.Empty);
            }
            foreach (var item in Events)
            {
                tem+=item.GetScript(string.Empty);
            }
            return tem;
        }


        public string GetReferences(string reference)
        {
            return Reference;
        }
        public string GetLinks(string links)
        {
            return this.Links;
        }


        public void GetFiles(List<string> files)
        {
            files.Add(this.File);
        }
    }
}
